package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.example.common.core.utils.MapstructUtils;
import org.example.common.core.utils.page.Pager;
import org.example.common.datasource.utils.SearchParamsHelper;
import org.example.system.domain.GuidGenerator;
import org.example.system.domain.SysOauthClient;
import org.example.system.domain.bo.SysClientBo;
import org.example.system.domain.vo.SysClientVo;
import org.example.system.domain.vo.SysOauthClientVo;
import org.example.system.mapper.SysOauthClientMapper;
import org.example.system.service.ISysOauthClientService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@RequiredArgsConstructor
public class SysOauthClientServiceImpl implements ISysOauthClientService {

    private final SysOauthClientMapper baseMapper;
    private final PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();


    @Override
    public SysClientVo preBuild() {
        SysClientVo oauthClientDetails=new SysClientVo();
        oauthClientDetails.setClientId(GuidGenerator.generate());
        oauthClientDetails.setClientSecret(GuidGenerator.generateClientSecret());
        return oauthClientDetails;
    }


    @Override
    public int createOauthClientDetails(SysClientBo clientDetails) {
        SysOauthClient sysOauthClient = MapstructUtils.convert(clientDetails.createDomainBo(), SysOauthClient.class);
        sysOauthClient.setClientSecret(passwordEncoder.encode(clientDetails.getClientSecret()));
        sysOauthClient.setClientSecretPlaintext(clientDetails.getClientSecret());
        return baseMapper.insertOrUpdate(sysOauthClient)?1:0;
    }

    @Override
    public int updateOauthClientDetails(SysClientBo clientDetails) {
        SysOauthClient sysOauthClient = MapstructUtils.convert(clientDetails.createDomainBo(), SysOauthClient.class);
        sysOauthClient.setClientSecret(null);
        return baseMapper.updateById(sysOauthClient);
    }

    @Override
    public int deleteOauthClientDetails(String clientId) {
        return  baseMapper.deleteById(clientId);
    }

    @Override
    public String resetClientSecret(String clientId) {
        String secret= GuidGenerator.generateClientSecret();
        LambdaUpdateWrapper updateWrapper= Wrappers.lambdaUpdate(SysOauthClient.class)
                .set(SysOauthClient::getClientSecret,passwordEncoder.encode(secret))
                .set(SysOauthClient::getClientSecretPlaintext,secret)
                .eq(SysOauthClient::getClientId,clientId);
        boolean flag=baseMapper.update(null,updateWrapper)>0;
        if(flag){
            return secret;
        }
        return null;
    }

    @Override
    public Pager<SysClientVo>  selectOauthClientPage(Pager pager) {
        SearchParamsHelper.loadSearchParam(pager,Wrappers.query(),(k,w)-> baseMapper.selectVoPage(k,w));
        List<SysOauthClientVo> list=pager.getRows();
        if(list.size()>0){
            pager.setRows(SysClientVo.build(list));
        }
        return pager;
    }

    @Override
    public List<SysClientVo> selectOauthClientList(Pager pager) {
        QueryWrapper wrapper= Wrappers.query();
        SearchParamsHelper.loadSearchParam(pager,wrapper);
        List<SysOauthClientVo> list= baseMapper.selectVoList(wrapper);
        return SysClientVo.build(list);
    }

    @Override
    public SysClientVo selectOauthClientByClientId(String clientId) {
        SysOauthClientVo oauthClientVo= baseMapper.selectVoById(clientId);
        return SysClientVo.build(oauthClientVo);
    }
}
